home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZDebug.cpp
< prev
next >
Wrap
Text File
|
1997-08-15
|
8KB
|
404 lines
/*
* File: ZDebug.cpp
* Summary: Debugging functions and macros.
* Written by: Jesse Jones
*
* Copyright ゥ 1996-1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <6> 8/14/97 JDJ TRACE functions use a larger buffer.
* <5> 6/12/97 JDJ BreakStrToDebugger strips trailing carriage returns.
* <4> 1/03/97 JDJ Added a comment to operator new override.
* <3> 12/10/96 JDJ Turned off intense debugging.
* <2> 12/05/96 JDJ Exposed the break to debugger functions.
* <1> 1/13/96 JDJ Created.
*/
#include <ZDebug.h>
#include <String>
#include <String.h>
#include <StdArg.h>
#include <StdIo.h>
#include <AERegistry.h>
#include <AppleEvents.h>
#include <Controls.h>
#include <Memory.h>
#include <Processes.h>
#include <TextEdit.h>
#include <Types.h>
#include <Windows.h>
#include <ZTrace.h>
#include <ZTypes.h>
#if ASSERTS_THROW
#include <ZExceptions.h>
#endif
//---------------------------------------------------------------
//
// Globals
//
//---------------------------------------------------------------
bool gMonkeyLives = false;
#if DEBUG
bool gIntenseDebugging = false;
bool gBreakOnAssert = true;
extern "C" Boolean SIOUXIsAppWindow(const WindowRef); // ・・・ハundocumented SIOUX function...
#endif
//---------------------------------------------------------------
//
// BreakToDebugger
//
//---------------------------------------------------------------
void BreakToDebugger()
{
#ifdef powerc
Debugger();
#else
Debugger68k();
#endif
}
//---------------------------------------------------------------
//
// BreakStrToDebugger
//
//---------------------------------------------------------------
void BreakStrToDebugger(const char* mesg)
{
unsigned char s[256];
ulong len = strlen(mesg);
if (len > 255)
len = 255;
s[0] = (Byte) len;
BlockMoveData(mesg, s+1, len);
// Strip off trailing carriage returns.
while (s[0] > 0 && (s[s[0]] == '¥n' || s[s[0]] == '¥r'))
--s[0];
#if powerc
DebugStr(s);
#else
DebugStr68k(s);
#endif
}
//---------------------------------------------------------------
//
// AssertFailed
//
//---------------------------------------------------------------
#if DEBUG || ASSERTS_THROW
void AssertFailed(const char* expr, const char* file, int line)
{
char mesg[256];
sprintf(mesg, "ASSERT(%s) in %s at line %d failed.", expr, file, line);
#if DEBUG
if (gBreakOnAssert)
BreakStrToDebugger(mesg);
#endif
#if ASSERTS_THROW
throw TAssertException(mesg);
#endif
}
#endif // DEBUG || ASSERTS_THROW
#if DEBUG
//---------------------------------------------------------------
//
// DEBUGSTR
//
//---------------------------------------------------------------
void DEBUGSTR(const char* format, ...)
{
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
BreakStrToDebugger(mesg);
}
//---------------------------------------------------------------
//
// DEBUGSTR_IF
//
//---------------------------------------------------------------
void DEBUGSTR_IF(bool cond, const char* format, ...)
{
if (cond) {
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
BreakStrToDebugger(mesg);
}
}
//---------------------------------------------------------------
//
// TRACE
//
//---------------------------------------------------------------
void TRACE(const char* format, ...)
{
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
UTraceFlow::Trace(mesg);
}
//---------------------------------------------------------------
//
// TRACE_IF
//
//---------------------------------------------------------------
void TRACE_IF(bool cond, const char* format, ...)
{
if (cond) {
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
UTraceFlow::Trace(mesg);
}
}
//---------------------------------------------------------------
//
// TRACEFLOW
//
//---------------------------------------------------------------
void TRACEFLOW(const char* category, const char* format, ...)
{
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
UTraceFlow::TraceFlow(category, mesg);
}
//---------------------------------------------------------------
//
// TRACEFLOW_IF
//
//---------------------------------------------------------------
void TRACEFLOW_IF(bool cond, const char* category, const char* format, ...)
{
if (cond) {
char mesg[2048];
va_list args;
va_start(args, format);
vsprintf(mesg, format, args);
va_end(args);
UTraceFlow::TraceFlow(category, mesg);
}
}
//---------------------------------------------------------------
//
// Length
//
//---------------------------------------------------------------
#pragma profile off
static long Length(const char* str) // interrupt code
{
long len = 0;
while (*str++)
len++;
return len;
}
#pragma profile reset
//---------------------------------------------------------------
//
// Append (char*, const char*, long)
//
//---------------------------------------------------------------
#pragma profile off
static void Append(char* buffer, const char* str, long bufferLen) // interrupt code
{
if (buffer != nil && str != nil) {
long index = Length(buffer);
while (*str && index+1 < bufferLen)
buffer[index++] = *str++;
buffer[index] = '¥0';
}
}
#pragma profile reset
//---------------------------------------------------------------
//
// Append (char*, long, long)
//
//---------------------------------------------------------------
#pragma profile off
static void Append(char* buffer, long num, long bufferLen) // interrupt code
{
if (buffer != nil) {
// Convert num to a str starting at index.
char str[12];
str[11] = '¥0';
bool negative = num < 0;
short index = 11;
do {
long digit = num % 10;
num /= 10;
str[--index] = (char) ('0' + (negative ? -digit : digit));
} while (num != 0 && index > 0);
if (negative && index > 0)
str[--index] = '-';
// Do the concatenation
Append(buffer, str + index, bufferLen);
}
}
#pragma profile reset
//---------------------------------------------------------------
//
// SAFE_DEBUGSTR (const char*)
//
//---------------------------------------------------------------
#pragma profile off
void SAFE_DEBUGSTR(const char* str) // interrupt code
{
unsigned char mesg[81]; // MACSBUG seems to choke on large strings...
long len = Length(str);
if (len > 80)
len = 80;
mesg[0] = (Byte) len;
BlockMoveData(str, mesg+1, len);
#ifdef powerc
DebugStr(mesg);
#else
SysBreakStr(mesg);
#endif
}
#pragma profile reset
//---------------------------------------------------------------
//
// SAFE_DEBUGSTR (const char*)
//
//---------------------------------------------------------------
#pragma profile off
void SAFE_DEBUGSTR(const char* mesg, long num) // interrupt code
{
char buffer[256];
buffer[0] = '¥0';
Append(buffer, mesg, 256);
Append(buffer, num, 256);
SAFE_DEBUGSTR(buffer);
}
#pragma profile reset
//---------------------------------------------------------------
//
// SafeAssertFailed
//
//---------------------------------------------------------------
#pragma profile off
void SafeAssertFailed(const char* expr, const char* file, int line) // interrupt code
{
char buffer[256];
buffer[0] = '¥0';
Append(buffer, "ASSERT(", 256);
Append(buffer, expr, 256);
Append(buffer, ") in ", 256);
Append(buffer, file, 256);
Append(buffer, " at line ", 256);
Append(buffer, line, 256);
Append(buffer, " failed.", 256);
SAFE_DEBUGSTR(buffer);
}
#pragma profile reset
//---------------------------------------------------------------
//
// GetSIOUXWindow
//
// If you're using console.stubs.c you may need to add SIOUXIsAppWindow
// to console.stubs.c (just return false).
//
//---------------------------------------------------------------
WindowRef GetSIOUXWindow()
{
WindowRef window = FrontWindow();
while (window != nil && !SIOUXIsAppWindow(window))
window = GetNextWindow(window);
return window;
}
#endif // DEBUG